<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../刘佳富/Css/reset.css">
    <style>
        .box {
            margin: 100px auto;
            width: 500px;
            height: 50px;
            border: 1px solid black;
        }

        ul {
            display: flex;
            justify-content: space-between;
        }

        .list {
            width: 80px;
            height: 50px;
            text-align: center;
            line-height: 50px;
        }

        .active {
            color: aqua;
        }
    </style>
</head>

<body>
    <div class="box">
        <ul>
            <li class="list">你好</li>
            <li class="list">王振</li>
            <li class="list">王者荣耀</li>
            <li class="list">电视剧</li>
            <li class="list">中医节目</li>
            <li class="list">综艺节目</li>
        </ul>
    </div>

    <script>
        //使用事件委托
        const ul = document.querySelector('ul')
        const list = document.querySelector('.list')
        ul.addEventListener('click', function (e) {
            // console.log(e.target.tagName);
            if (e.target.tagName === 'LI') {
                const active = document.querySelector('.active')
                //原来li里面没有active直接添加就会报错，要添加一个判断如果是null就把active删除 先添加active，
                if (active) {
                    active.classList.remove('active')
                }
                e.target.classList.add('active')

            }

        })
    </script>
</body>

</html>